package lz.controls
{
	import com.common.utils.Reflection;
	
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.utils.Timer;

	/**
	 *固定皮肤滚动条 
	 * @author lake
	 * 
	 */	
	public class LLScrollBar extends ScrollBar
	{
		/**
		 * 滑块
		 */		
		private var __dragBar	:SimpleButton;
		/**
		 * track 背景区域
		 */		
		private var __area		:Sprite;
		/**
		 * 下按钮
		 */		
		private var __downBtn	:SimpleButton;
		/**
		 * 上按钮
		 */		
		private var __upBtn	:SimpleButton;
		/**
		 *最小高度 
		 */		
		public static var MINHEIGHT		:Number;
		/**
		 *间隔 
		 */		
		public static const SCROLLGRAP 	:Number = 0;
		
		public function LLScrollBar()
		{
			super();
		}
		/**
		 * 初始化
		 * @private (protected)
		 */		
		override protected function configUI():void 
		{
			//super.configUI();
			_symbolSkin = Reflection.createInstance("SkinScrollBar");
			if(_symbolSkin == null)
			{
				return
			}
			this.addChild(_symbolSkin);
			__dragBar	= _symbolSkin.__dragBar;
			__area 		= _symbolSkin.__area;
			__downBtn 	= _symbolSkin.__downBtn;
			__upBtn 	= _symbolSkin.__upBtn;
			
			_width 		= _symbolSkin.width;
			_height 	= _symbolSkin.height;
			layout();
			MINHEIGHT 	= __downBtn.height + __upBtn.height + 10;
			__upBtn.addEventListener(MouseEvent.CLICK,scrollPressHandlerP,false,0,true);
			__downBtn.addEventListener(MouseEvent.CLICK,scrollPressHandlerP,false,0,true);
			__area.addEventListener(MouseEvent.CLICK,scrollPressHandlerP,false,0,true);
			__dragBar.addEventListener(MouseEvent.MOUSE_DOWN,thumbPressHandler,false,0,true);
		}
		/**
		 * 点击事件
		 * @private (protected)
		 */
		protected function scrollPressHandlerP(event:Event):void 
		{
			if (event.currentTarget == __upBtn) 
			{
				setScrollPosition(scrollPosition-lineScrollSize); 
			} else if (event.currentTarget == __downBtn)
			{
				setScrollPosition(scrollPosition+lineScrollSize);
			} else
			{
				var pos:Number = Math.max(minScrollPosition,
					Math.min((mouseY - (__upBtn.height + SCROLLGRAP))/__area.height * maxScrollPosition,maxScrollPosition));
				scrollPosition -= (scrollPosition - Math.round(pos));
			}
		}
		/**
		 * 滑块事件
		 */
		override protected function thumbPressHandler(event:MouseEvent):void 
		{
			stage.addEventListener(MouseEvent.MOUSE_MOVE,dragMove,false,0,true);			
			stage.addEventListener(MouseEvent.MOUSE_UP,stopMove,false,0,true);	
		}
		/**
		 * dragMove
		 * @param e
		 * 
		 */		
		private function dragMove(e:MouseEvent):void
		{
			var pos:Number = Math.max(minScrollPosition,
				Math.min((mouseY - (__upBtn.height + SCROLLGRAP))/__area.height * maxScrollPosition,maxScrollPosition));
			scrollPosition = Math.round(pos);
		}
		/**
		 * stopMove
		 * @param e
		 * 
		 */		
		private function stopMove(e:MouseEvent):void
		{
			if(stage.hasEventListener(MouseEvent.MOUSE_MOVE))
			{
				stage.removeEventListener(MouseEvent.MOUSE_MOVE,dragMove);
			}
			stage.removeEventListener(MouseEvent.MOUSE_UP,stopMove);			
		}
		/**
		 * 处理滑块拖动
		 * @private (protected)
		 */
		override protected function handleThumbDrag(event:MouseEvent):void 
		{
			
		}
		
		/**
		 * 处理滑块松开
		 */
		override protected function thumbReleaseHandler(event:MouseEvent):void 
		{
			
		}
		/**
		 * 更新滑块位置和大小
		 * @private
		 */
		override protected function updateThumb():void 
		{
			if(_symbolSkin == null)
			{
				return;
			}
			layout();
		}
		/**
		 * layout
		 * 
		 */		
		protected function layout():void
		{
			__area.y = __upBtn.y + __upBtn.height + SCROLLGRAP;
			__area.height = this._height - __upBtn.height - __downBtn.height - 2 * SCROLLGRAP;
			
			__downBtn.y =  __area.height + __area.y + __downBtn.height + SCROLLGRAP;
			scrollPosition = Math.max(minScrollPosition,Math.min(scrollPosition,maxScrollPosition));
			__dragBar.y = (__upBtn.height + SCROLLGRAP) 
				+ ((scrollPosition -minScrollPosition)/(maxScrollPosition - minScrollPosition)) * (__area.height - __dragBar.height);
		}
		/**
		 *获取 设置 滚动条高度 
		 * @param value
		 * 
		 */		
		override public function set height(value:Number):void
		{
			_height = Math.max(MINHEIGHT,value);
			layout();
		}
		override	public function get height():Number
		{
			return _height;
		}
	}
}